/*
 * @lc app=leetcode.cn id=523 lang=golang
 *
 * [523] 连续的子数组和
 */
package Solutions

// @lc code=start
func checkSubarraySum(nums []int, k int) bool {
	if len(nums) < 2 {
		return false
	}
	var preSum = make([]int, len(nums))
	sum := 0
	for i := 0; i < len(nums); i++ {
		sum += nums[i]
		preSum[i] = sum
	}
	var mp = make(map[int]struct{}, len(nums))
	mp[0] = struct{}{}
	if preSum[1]%k == 0 {
		return true
	}
	for i := 2; i < len(preSum); i++ {
		mp[preSum[i-2]] = struct{}{}
		t := preSum[i]
		for j := 0; t >= 0; j++ {
			if _, ok := mp[t]; ok {
				return true
			}
			t -= k
		}

	}
	return false
}

// @lc code=end
